﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MindFusion.Scheduling;
using System.Collections.ObjectModel;

namespace DentistApplication
{
    /// <summary>
    /// Interaction logic for TimetableWindow.xaml
    /// </summary>
    public partial class TimetableWindow : Window
    {
        ObservableCollection<Patient> _patientCollection = new ObservableCollection<Patient>();
        String[] months = { "styczeń", "luty", "marzec", "kwiecień", "maj", "czerwiec", "lipiec", "sierpień", "wrzesień", "październik", "listopad", "grudzień" };
        String[] days = { "pon", "wt", "śr", "czw", "pt", "sob", "nd" };
        bool hidePopWindow = false;
        public ObservableCollection<Patient> patientCollection
        { get { return _patientCollection; } }

        public TimetableWindow()
        {
            InitializeComponent();
            initializeTerminal();
        }

        #region TERMINAL

        private void initializeTerminal()
        {
            // Synchronize both views
            _schedule.Schedule = _calendar.Schedule;

            // Select the current date
            _calendar.Selection.Set(DateTime.Today);

            fillTimetableWithAppointments();
        }
        void _calendar_CellPresenterCreated(object sender, CellPresenterEventArgs e)
        {
            Style style = FindResource("myStyle") as Style;
            DateTime start = e.CellPresenter.StartTime;
            DateTime end = e.CellPresenter.StartTime;
            end = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59);
            int itemCount = _schedule.Schedule.GetAllItems(start, end).Count;
            if (itemCount != 0)
                e.CellPresenter.Style = style;
        }

        void _schedule_ItemDeleted(object sender, MindFusion.Scheduling.ItemEventArgs e)
        {
            // Check if there are more items on this date.
            // If no - remove the custom style
            DateTime start = e.Item.StartTime.Date;
            DateTime end = e.Item.EndTime;
            end = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59);
            if (_schedule.Schedule.GetAllItems(start, end).Count == 0)
            {
                _calendar.BeginInit();
                _calendar.EndInit();
            }
        }

        void _schedule_ItemCreated(object sender, MindFusion.Scheduling.ItemEventArgs e)
        {
            _calendar.BeginInit();
            _calendar.EndInit();
        }

        void Selection_Changed(object sender, EventArgs e)
        {
            if (_calendar.Selection.IsEmpty)
            {
                _schedule.TimetableSettings.Dates.Clear();
                return;
            }

            _schedule.BeginInit();
            _schedule.TimetableSettings.Dates.Clear();
            _schedule.TimetableSettings.Dates.Add((DateTime)_calendar.Selection.Ranges[0]);
            _schedule.EndInit();
        }
        #endregion






        private void _schedule_MouseUp(object sender, MouseButtonEventArgs e)
        {

            IList<DateTime> lt = _schedule.Selection.Ranges;
            if (lt.Count > 1)
            {
                DateTime startTime = lt[0];
                DateTime endTime = lt[1];

                string when = getDay(startTime.DayOfWeek.ToString()) + ", " + startTime.Day + " " + months[startTime.Month - 1] + " " +
                    startTime.ToShortTimeString() + " - " + endTime.ToShortTimeString();

                openAppointmentPop();
                la_popWhen.Content = when;

                la_popWhen.Tag = startTime;
                la_popEdit.Tag = endTime;
            }
        }

        private string getDay(string day)
        {
            if (day.Equals("Monday")) return days[0];
            if (day.Equals("Tuesday")) return days[1];
            if (day.Equals("Wednesday")) return days[2];
            if (day.Equals("Thursday")) return days[3];
            if (day.Equals("Friday")) return days[4];
            if (day.Equals("Saturday")) return days[5];
            return days[6];

        }

        private void pop_appointment_MouseLeave(object sender, MouseEventArgs e)
        {
            hidePopWindow = true;
        }


        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (hidePopWindow)
                pop_appointment.IsOpen = false;
            hidePopWindow = false;
        }

        private void pop_appointment_MouseEnter(object sender, MouseEventArgs e)
        {
            hidePopWindow = false;
        }

        private void bt_popAdd_Click(object sender, RoutedEventArgs e)
        {
            addNewAppointment();
            hidePopWindow = false;
        }

        private void addNewAppointment()
        {
            DateTime startTime = (DateTime)la_popWhen.Tag;
            DateTime endTime = (DateTime)la_popEdit.Tag;
            int patient_id = 0;
            int dentist_id = 0;
            String notes = "Pacjent: " + box_popName.Text + " " + box_popLastName.Text;

            if (TimetableHandler.addAppointment('n', startTime, endTime, patient_id, dentist_id, notes))
            {
                _calendar.BeginInit();

                Appointment ap = new Appointment()
                {
                    StartTime = startTime,
                    EndTime = endTime,
                    HeaderText = la_popWhen.Content.ToString(),
                    DescriptionText = notes,
                };

                _calendar.Schedule.Items.Add(ap);

                _calendar.EndInit();
                pop_appointment.IsOpen = false;

            }
            else
                MessageBox.Show("Wystapil blad");

        }

        private void fillTimetableWithAppointments()
        {
            List<Timetable> tt = TimetableHandler.getAllAppointments();

            _calendar.BeginInit();

            foreach (Timetable t in tt)
            {
                string when = getDay(t.startDate.DayOfWeek.ToString()) + ", " + t.startDate.Day + " " + months[t.startDate.Month - 1] + " " +
                 t.startDate.ToShortTimeString() + " - " + t.endDate.ToShortTimeString();

                Appointment ap = new Appointment()
                {
                    StartTime = t.startDate,
                    EndTime = t.endDate,
                    HeaderText = when,
                    DescriptionText = t.notes,
                };

                _calendar.Schedule.Items.Add(ap);

            }

            _calendar.EndInit();
        }

        private void bt_popSearchPatient_Click(object sender, RoutedEventArgs e)
        {

            openPatientSearchPop();
        }






        #region RIBBONS AND PANELS

        private void hideAllPanels()
        {
            panel_timetableDual.Visibility = Visibility.Hidden;
            pop_appointment.IsOpen = false;
            pop_searchPatient.IsOpen = false;
        }
        private void ribbon_showTimetable_Click(object sender, RoutedEventArgs e)
        {
            //OLD
            hideAllPanels();
            panel_timetableDual.Visibility = System.Windows.Visibility.Visible;
        }

       
        #endregion

        private void list_patientsSearch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Patient p = (Patient)list_patientsSearch.SelectedValue;

            if (p != null)
            {
                box_popName.Text = p.firstname;
                box_popLastName.Text = p.lastname;
                box_popPesel.Text = p.pesel;
                box_popName.Tag = p;
                pop_searchPatient.IsOpen = false;
                pop_appointment.IsOpen = true;
            }
        }

        private void box_searchPatient_TextChanged(object sender, TextChangedEventArgs e)
        {
            string name = popbox_popSearchPatName.Text;
            string lastname = popbox_popSearchPatLastName.Text;
            string pesel = popbox_popSearchPesel.Text;

            if (name.Length > 3 || lastname.Length > 3 || pesel.Length > 3)
            {
                _patientCollection.Clear();
                List<Patient> lp = PatientsHandler.searchForPatients(name, lastname, pesel);
                lp.ForEach(x => _patientCollection.Add(x));

            }
        }

        private void openAppointmentPop()
        {
           // box_popLastName.Clear();
            //box_popName.Clear();
            //box_popPesel.Clear();
            pop_appointment.IsOpen = true;
        }

        private void openPatientSearchPop()
        {
            hidePopWindow = false;
            popbox_popSearchPatLastName.Clear();
            popbox_popSearchPatName.Clear();
            popbox_popSearchPesel.Clear();
            _patientCollection.Clear();
            pop_searchPatient.IsOpen = true;

        }

        private void box_SearchDentName_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void ribbon_viewMonth_MouseUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void ribbon_viewWeek_MouseUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void ribbon_viewDual_MouseUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void bt_popSearchPatCANCEL_Click(object sender, RoutedEventArgs e)
        {
            pop_searchPatient.IsOpen = false;
        }

    }
}
